package com.ldgame.utils;

public class Timer {
	private static long time = 0;
	private static int fps = 0;
	private static int	nFrames = 0;
	private static float loopTime = 0.0f;
	private static float appTime = 0.0f;
	private static float fpsTimer = 0.0f;
	
	public static int currentFPS = 0;
	
	public static void syncToSystemTime() {
		time = System.nanoTime();
	}

	public static float getLoopTime() {
		return loopTime;
	}

	public static float getAppTime() {
		return appTime;
	}
	
	public static void calcLoopTime() {
		loopTime = (float)(System.nanoTime() - time) / 1000000000.0f;
		appTime += loopTime;
	}
	
	public static void calcFPS() {
		if (fpsTimer > 1.0f) {
			currentFPS = fps / nFrames;
			// TODO print (fps/nFrames) somewhere
			fpsTimer = 0.0f;
			fps = 0;
			nFrames = 0;
		}
		
		fpsTimer += loopTime;
		fps += (int)(1.0f / loopTime);
		++nFrames;
	}
}
